package com.slm.report.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.slm.mylibrary.base.BaseMvpActivity;
import com.slm.report.R;
import com.slm.report.databinding.ActivityStudentInfoBinding;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.db.TabEntity;
import com.slm.report.state.StudentInfoViewModel;
import com.slm.report.ui.fragment.CalendarFragment;
import com.slm.report.ui.fragment.DegreeListFragment;
import com.slm.report.ui.fragment.ReportFragment;
import com.slm.report.ui.fragment.StudentInfoFragment;
import com.slm.report.ui.fragment.VisionListFragment;

import java.util.ArrayList;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.ui
 * @ClassName: StudentInfoActvitiy
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/5/30 19:10
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/30 19:10
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class StudentInfoActivity extends BaseMvpActivity<StudentInfoViewModel> {
    private ActivityStudentInfoBinding viewBinding;
    private String[] mTitles = {"基本信息", "视力记录", "度数记录", "打卡记录", "情况报表"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private boolean isChange=false;

    public static Intent getIntent(Context context, StudentInfoBean infoBean) {
        Intent intent = new Intent(context, StudentInfoActivity.class);
        intent.putExtra("data", infoBean);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityStudentInfoBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        StudentInfoBean data = (StudentInfoBean) getIntent().getSerializableExtra("data");
        mViewModel.studentInfoBean.setValue(data);
        mViewModel.isChangeData.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                isChange = aBoolean;
            }
        });
        fragments.add(new StudentInfoFragment());
        fragments.add(new VisionListFragment());
        fragments.add(new DegreeListFragment());
        fragments.add(new CalendarFragment());
        fragments.add(new ReportFragment());
        viewBinding.commonTab.setTabData(mTabEntities, this, R.id.fragment_container, fragments);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
           if(isChange){
               setResult(RESULT_OK);
               finish();
           }else{
               finish();
           }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
